﻿using System;
using SlamCms.Data;
using SlamCms.Services;

namespace SlamCms.Common
{
	public class SlamCmsConfiguration : IDisposable
	{
		private static readonly SlamCmsConfiguration _instance = new SlamCmsConfiguration();
		private IDependencyResolver _dependencyResolver = EmptyResolver.Instance;
		private readonly ContentTypeCollection _contentTypeCollection = GetDefaultContentTypes();
		private bool _disposed;

		static SlamCmsConfiguration()
		{
		}

		private SlamCmsConfiguration()
		{
			this.Services = new DefaultServices(this);
		}

		public static SlamCmsConfiguration Instance
		{
			get
			{
				return _instance;
			}
		}

		public ContentTypeCollection ContentTypes
		{
			get
			{
				return _contentTypeCollection;
			}
		}

		public string ConnectionString { get; set; }

		public DefaultServices Services { get; internal set; }

		public IDependencyResolver DependencyResolver
		{
			get
			{
				return this._dependencyResolver;
			}
			set
			{
				if (value == null)
					throw new ArgumentNullException("value");

				this._dependencyResolver = value;
			}
		}

		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize((object)this);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (this._disposed)
				return;
			this._disposed = true;
			if (!disposing)
				return;

			this.Services.Dispose();
			//this.DependencyResolver.Dispose();
		}

		private static ContentTypeCollection GetDefaultContentTypes()
		{
			var contentItemType = typeof(ContentItem);
			return new ContentTypeCollection().Add(TypeHelpers.GetFilteredTypesFromAssemblies(t => contentItemType.IsAssignableFrom(t) && t != contentItemType));
		}
	}
}
